<?php

class DbPrimaryEntity extends DbEntity
{
    public function onExtracting( $id, array $val, IDbMapper $chain )
    {
        if( ! isset( $val[ self::KEY_PRIMARY ] ) ) return false; 
        
        if( ! is_string( $val[self::KEY_PRIMARY] ) OR empty( $val[self::KEY_PRIMARY] ) ) 
	    {
	        throw new Exception( 'Invalid primary key specified. Only string is allowed' );
	    }
	    
	    if( ! is_string( $val[self::KEY_TABLE] ) OR empty( $val[self::KEY_TABLE] ) ) 
	    {
	        throw new Exception( 'Invalid table name specified. Only string is allowed' );
	    }
	    
	    $chain->properties[ $id ] = '';
        $chain->primary = $val[self::KEY_PRIMARY];
        $chain->table = $val[self::KEY_TABLE];
        $chain->primeAlias = isset( $val[self::KEY_PRIMARY_ALIAS] )?
            $val[self::KEY_PRIMARY_ALIAS]:
            $val[self::KEY_PRIMARY] ;
        
        $chain->fieldMaps[ $val[self::KEY_PRIMARY] ] = $id;
        if( isset( $val[self::KEY_IS_STRING] ) AND $val[self::KEY_IS_STRING] )
		{
			$chain->isString = true;
		}
    }
}
